package server

import (
	"227zk/common/models/mysql"
	"227zk/common/models/redis"
	"227zk/common/proto/user"
	"context"
	"fmt"
	"time"
)

func Login(ctx context.Context, in *user.UserReq) (interface{}, error) {
	getUser, err := mysql.GetUser(in.Phone)
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	if getUser.Phone == "" {
		m := mysql.User{
			Phone:    in.Phone,
			Password: in.Password,
		}
		err := mysql.Create(m)
		if err != nil {
			return nil, fmt.Errorf("注册失败")
		}
	}
	if getUser.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return getUser.ID, nil
}
func List(ctx context.Context, in *user.ListReq) (interface{}, error) {
	list, err := mysql.List()
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var lists []*user.List
	for _, u := range list {
		lists = append(lists, &user.List{
			Phone:    u.Phone,
			Password: u.Password,
		})
	}

	redis.Rdb.Set(context.Background(), "list1", lists, time.Minute*24)
	return lists, err
}
